<?php

namespace app\admin\controller;

class Login extends Common {

        public function _initialize() {
                
        }

        /**
         * 登陆
         */
        public function login() {
                if (get_member_id() <= 0) {
                        if (IS_POST) {
                                if (empty($_POST['account'])) {
                                        return $this->error('帐号错误！');
                                } elseif (empty($_POST['password'])) {
                                        return $this->error('密码必须！');
                                }
                                //验证码验证
                                $verify = new \org\Verify();
                                if (!$verify->check(I('post.verify'), "admin")) {
                                        return $this->error("验证码错误!");
                                }
                                /*
                                 * @ 认证条件
                                 */
                                $map = array();
                                $map['username'] = I('post.account');
                                //非禁用账号
                                $map['status'] = 1;
                                $uesr = M("Member");
                                $authInfo = $uesr->where($map)->find();
                                //使用用户名、密码和状态的方式进行认证
                                if (false === $authInfo) {
                                        return $this->error('帐号错误或已禁用！');
                                } else {
                                        if ($authInfo['password'] != pwHash(I("post.password"))) {
                                                return $this->error('帐号或密码错误！');
                                        }
                                        session('mid', $authInfo['id']);
                                        session('mname', $authInfo['realname']);
                                        //保存登录信息
                                        $User = M('Member');
                                        $time = time();
                                        $data = array();
                                        //$data['id'] = $authInfo['id'];
                                        $data['lastlogintime'] = $time;
                                        $data['logintimes'] = array('exp', 'logintimes+1');
                                        $data['lastloginip'] = get_client_ip();
                                        $User->where(['id' => $authInfo['id']])->save($data);
                                        $this->redirect('Index/index');
                                }
                        } else {
                                return $this->fetch();
                        }
                } else {
                        $this->redirect('Index/index');
                }
        }
        
        public function login_dialog(){
                return $this->fetch();
        }
        /**
         * 注销账号
         */
        public function loginout() {
                session(null);
                return $this->redirect('Login/login');
        }

        /**
          +----------------------------------------------------------
         * 显示验证码
          +----------------------------------------------------------
         */
        public function verify() {
                $config = array(
                    'fontSize' => 12, // 验证码字体大小    
                    'length' => 3, // 验证码位数    
                    'useNoise' => false, // 关闭验证码杂点
                    'imageW' => 110,
                    'imageH' => 34,
                    'codeSet' => '0123456789',
                    'fontSize' => 16,
                    'fontttf' => '5.ttf'
                );
                $Verify = new \org\Verify($config);
                $Verify->entry('admin');
        }

}
